package com.zgj.netty_udp.utils;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 用来进行数据转换的类
 *
 * @author qs
 * 2021.3.16
 */
public class Convertor {
    /**
     * 字节数组向对象进行转化
     *
     * @param bytes 输入的byte数组
     * @param clazz 需要创建的对象类
     * @param <T>   泛型
     * @return 转换后的对象
     */
    public static <T> T byteToObject(byte[] bytes, Class<T> clazz) {
        T instance = null;
        try {
            instance = clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("实例化类失败", e);
        }

        List<Field> fields = new ArrayList<>();
        getFields(clazz, fields);
        ByteBuf buffer = Unpooled.buffer().writeBytes(bytes);
        for (Field field : fields) {
            fillData(field, instance, buffer);
        }

        return instance;
    }

    /**
     * 获取对象的变量
     *
     * @param clazz 类
     * @param list  变量存储列表
     * @return 变量列表
     */
    private static List<Field> getFields(Class clazz, List list) {
        Field[] fields = clazz.getDeclaredFields();
        list.addAll(Arrays.asList(fields));

        Class clazzSuper = clazz.getSuperclass();
        if (clazzSuper != null)
            getFields(clazzSuper, list);
        return list;
    }

    /**
     * 通过类型赋值
     *
     * @param field    属性
     * @param instance 对象
     * @param buffer   数据
     */
    private static void fillData(Field field, Object instance, ByteBuf buffer) {
        field.setAccessible(true);

        String typeName = field.getType().getName();
        try {
            switch (typeName) {
                case "java.lang.Boolean":
                case "boolean":
                    boolean b = buffer.readBoolean();
                    field.set(instance, b);
                    break;
                case "java.lang.Character":
                case "char":
                    CharSequence charSequence = buffer.readCharSequence(2, Charset.forName("UTF-8"));
                    field.set(instance, charSequence);
                    break;
                case "java.lang.Byte":
                case "byte":
                    byte b1 = buffer.readByte();
                    field.set(instance, b1);
                    break;
                case "java.lang.Short":
                case "short":
                    short readShort = buffer.readShort();
                    field.set(instance, readShort);
                    break;
                case "java.lang.Integer":
                case "int":
                    int readInt = buffer.readInt();
                    field.set(instance, readInt);
                    break;
                case "java.lang.Long":
                case "long":
                    long l = buffer.readLong();
                    field.set(instance, l);
                    break;
                case "java.lang.Float":
                case "float":
                    float readFloat = buffer.readFloat();
                    field.set(instance, readFloat);
                    break;
                case "java.lang.Double":
                case "double":
                    double readDouble = buffer.readDouble();
                    field.set(instance, readDouble);
                    break;
                case "java.util.Map":
                    Map<Byte, Byte> map = new HashMap<>();
                    while (true) {
                        try {
                            byte aByte = buffer.readByte();
                            byte aByte1 = buffer.readByte();
                            map.put(aByte, aByte1);
                        } catch (Exception e) {
                            field.set(instance, map);
                            break;
                        }
                    }
                    break;
                case "java.util.List":
                    List<Long> list = new ArrayList<>();
                    while (true) {
                        try {
                            long param = buffer.readLong();
                            list.add(param);
                        } catch (Exception e) {
                            field.set(instance, list);
                            break;
                        }
                    }
                    break;
                default:
                    Object o = field.getType().newInstance();
                    for (Field fieldSub : field.getType().getDeclaredFields()) {
                        fillData(fieldSub, o, buffer);
                    }
                    field.set(instance, o);
                    break;
            }
        } catch (Exception e) {
            throw new RuntimeException(typeName + "读取失败，field:" + field.getName(), e);
        }
    }

    /**
     * 对象转ByteBuf
     *
     * @param o   要转换的对象
     * @param <T> 类型
     * @return 转换结果
     */
    public static <T> byte[] ObjectToByte(T o) {
        List<Field> fields = new ArrayList<>();
        getFields(o.getClass(), fields);
        ByteBuf byteBuf = Unpooled.buffer();
        for (Field field : fields) {
            write2ByteBuf(field, o, byteBuf);
        }
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        return bytes;
    }

    /**
     * 为ByteBuf赋值
     *
     * @param field    属性
     * @param instance 对象
     * @param buffer   buffer
     */
    private static void write2ByteBuf(Field field, Object instance, ByteBuf buffer) {
        field.setAccessible(true);

        String typeName = field.getType().getName();

        Object value = null;
        try {
            value = field.get(instance);
        } catch (IllegalAccessException e) {
            new RuntimeException("反射获取值失败，filed:" + field.getName(), e);
        }
        if (value == null)
            return;
        switch (typeName) {
            case "java.lang.Boolean":
            case "boolean":
                buffer.writeBoolean((Boolean) value);
                break;
            case "java.lang.Character":
            case "char":
                buffer.writeCharSequence((CharSequence) value, Charset.forName("UTF-8"));
                break;
            case "java.lang.Byte":
            case "byte":
                buffer.writeByte((byte) value);
                break;
            case "java.lang.Short":
            case "short":
                buffer.writeShort((short) value);
                break;
            case "java.lang.Integer":
            case "int":
                buffer.writeInt((int) value);
                break;
            case "java.lang.Long":
            case "long":
                buffer.writeLong((long) value);
                break;
            case "java.lang.Float":
            case "float":
                buffer.writeFloat((float) value);
                break;
            case "java.lang.Double":
            case "double":
                buffer.writeDouble((double) value);
                break;
            case "java.lang.String":
                buffer.writeCharSequence((CharSequence) value, Charset.forName("UTF-8"));
                break;
            case "java.time.LocalDateTime":
                String s = ((LocalDateTime) value).toString();
                buffer.writeCharSequence((CharSequence) s, Charset.forName("UTF-8"));
                break;
            case "java.util.Map":
                try {
                    Map<Byte, Byte> map = (Map<Byte, Byte>) value;
                    for (byte key : map.keySet()) {
                        buffer.writeByte(key);
                        buffer.writeByte(map.get(key));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "java.util.List":
                try {
                    List<Long> list = (List<Long>) value;
                    for (long param : list) {
                        buffer.writeLong(param);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                for (Field fieldSub : field.getType().getDeclaredFields()) {
                    write2ByteBuf(fieldSub, value, buffer);
                }
                break;
        }
    }
}
